var ssc = null
var search_listId = -1;

$(function () {
    $('#login button').unbind('click'); //解除登录按钮默认的点击事件
    $('#login button').click(function (event) { //点击登录按钮
        $(this).parents('form').on_submit(function (e) {
            if(e.status && e.status == 1){
                location.reload();
            }
            console.log(e);
        });
    });

    $('#register .register .form_submit').unbind('click'); //解除登录按钮默认的点击事件
    $('#register .register .form_submit').click(function (event) { //点击登录按钮
        
        $(this).parents('form').on_submit(function (e) {
            if(e.status && e.status == 1){
                location.reload();
            }
            console.log(e);
        });
    });

    $('#register .register .radio label').click(function () {
        $('#register .register .radio label').removeClass('active');
        $(this).addClass('active');
    });

    $('#register .register .check_code button').click(function () {
        if ($('#register .register .check_code button').hasClass('ban')) {
            return;
        }
        var mobile = $('input[name=mobile]').val();
        if(!mobile){
            return 'Please enter the correct phone number';
        }
        $.ajax({
            url:"/api/Sms/getcode/mobile/" + mobile,
            success:function(res){
                if(res.code == 0){
                    $('#register .register .check_code button').addClass('ban');
                    var ms = 60;
                    $('#register .register .check_code button').html(ms + 's后重试');
                    var mstime = setInterval(function () {
                        ms -= 1;
                        if (ms <= 0) {
                            clearInterval(mstime);
                            $('#register .register .check_code button').removeClass('ban');
                            $('#register .register .check_code button').html('获取验证码');
                        }else{
                            $('#register .register .check_code button').html(ms + 's后重试');
                        }
                        
                    },1000);
                }else if(res.code == -1){

                }

            }
        })
    });

    $('#register').click(function () {
        $('#register').fadeOut(100);
    });
    $('#register .register').click(function (event) {
        event.stopPropagation();
    });
    
    $('#login_box').click(function () {
        $('#login_box').fadeOut(100);
    });
    $('#login_box .login_box').click(function (event) {
        event.stopPropagation();
    });

    $('.header .headss .input input').bind('keyup', function (e) { //输入框按下按键事件

        if ($(this).val() == '') { //输入框内容为空,清除推荐搜索词
            $('.header .headss .search_list').html('');
            $('.header .headss .search_list').hide();
            $('.header .headss .input .host_search').hide();
            return;
        }
        if (e.keyCode == "13") { //按下回车按钮
            if ($('.header .headss .search_list span.hover').length > 0) { //推荐搜索词有选中状态，吧选中状态的文本填充到搜索框  然后清除推荐搜索词
                search_listId = -1;
                $(this).val($('.header .headss .search_list span.hover b').html());
                $('.header .headss .search_list').hide();
                $('.header .headss .search_list').html('');
                $('.header .headss .input .host_search').hide();
                return;
            }
            //            console.log('搜索');
            headJumpSearch();
            return;
        }
        if (e.keyCode == "40") { //方向下按钮，
            //            console.log('方向下键');
            if (search_listId == $('.header .headss .search_list span').length - 1) {
                search_listId = -1;
            }
            search_listId += 1;
            $('.header .headss .search_list span').removeClass('hover');
            $('.header .headss .search_list span').eq(search_listId).addClass('hover');
            //            $(this).val($('.header .headss .search_list span.hover b').html());
            return;
        }
        if (e.keyCode == "38") {
            //            console.log('方向上键');
            if (search_listId == 0) {
                search_listId = $('.header .headss .search_list span').length;
            }
            search_listId -= 1;
            $('.header .headss .search_list span').removeClass('hover');
            $('.header .headss .search_list span').eq(search_listId).addClass('hover');
            //            $(this).val($('.header .headss .search_list span.hover b').html());
            return;
        }
        setHeadSearchList(ssc);
    });



    $('.header .headss .input input').bind('focus', function (e) { //搜索框获取焦点事件
        if ($(this).val() == '') {
            return;
        }
        setHeadSearchList(ssc);
    });
    $('.header .headss .input input').bind('blur', function (e) {
        if ($('.header .headss .search_list span.hover').length > 0) { //推荐搜索词有选中状态
            return;
        }
        search_listId = -1;
        $('.header .headss .search_list').hide();
        $('.header .headss .input .host_search').hide();
    });
    $('.header .headss .search_list').on('mouseover', 'span', function () {
        $('.header .headss .search_list span').removeClass('hover');
        search_listId = $(this).index('.header .headss .search_list span');
        $(this).addClass('hover');
    });
    $('.header .headss .search_list').on('mouseout', 'span', function () {
        $('.header .headss .search_list span').removeClass('hover');
        search_listId = -1;
    });
    $('.header .headss .search_list').on('click', '.hover', function () {
        $('.header .headss .input input').val($(this).find('b').html());
        $('.header .headss .search_list').html('');
        $('.header .headss .search_list').hide();
        $('.header .headss .input .host_search').hide();
    });

    $('.header .headss .input .select').click(function () {
        $('.header .headss .input .select').toggleClass('open');
        $('.header .headss .input .select .option').slideToggle(100);
    });
    $('.header .headss .input .select .option').click(function (event) {
        event.stopPropagation();
    });
    $('.header .headss .input .select .option li').click(function (event) {
        $('.header .headss .input .select .value').attr('value', $(this).attr('value'));
        $('.header .headss .input .select .value').html($(this).html() + '<i class="mdui-icon material-icons">play_arrow</i>');
        $('.header .headss .input .select').toggleClass('open');
        $('.header .headss .input .select .option').slideToggle(100);
    });
    $('.header .headss .input .button').click(function () {
        headJumpSearch();
    });

    var isinNav = false,
        isinSubNav = false;
    $('.header .head_nav_box li').on('mouseover',function(){
        isinNav = true;
        var index = $(this).index();
        submenuShow(index);
    });
    $('.header .head_nav_box li').on('mouseout',function(){
        isinNav = false
        var index = $(this).index();
        setTimeout(()=>{
            submenuHide(index);
        },50);
    });

    function submenuHide(index){
        $('#header .Submenu .item').eq(index).animate({
            height:'0px'
        });
    }
    function submenuShow(index){
        $('#header .Submenu .item').eq(index).animate({
            height:'70px'
        });
    }
})
